package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.sgb.kjwl.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

/**
 * @author younghow
 * @date 2021/8/3
 */
public class SalaryRegionPopWindow extends BasePopWindow implements View.OnClickListener {
    private RadioButton timeH, timeDay, timeMonth;
    private EditText inputData, mInputDataTwo;
    private String month, quarter, year;
    private String unitTxt = "小时";
    private String unitNo = "1";
    private int maxLength = 5;
    private int mMaxLengthTwo = 5;

    public SalaryRegionPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    public SalaryRegionPopWindow(Context context, int maxLength, int maxLengthTwo) {
        super(context, DIR_DOWN_UP);
        this.maxLength = maxLength;
        this.mMaxLengthTwo = maxLengthTwo;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_region_salary;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        inputData = holder.getPop().findViewById(R.id.input_data);
        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView resetView = holder.getPop().findViewById(R.id.reset_view);
        RadioGroup radioGroup = holder.getPop().findViewById(R.id.rd_rp);
        timeH = holder.getPop().findViewById(R.id.time_h);
        timeDay = holder.getPop().findViewById(R.id.time_day);
        timeMonth = holder.getPop().findViewById(R.id.time_month);
        mInputDataTwo = holder.getPop().findViewById(R.id.input_data_two);
        okView.setOnClickListener(this);
        resetView.setOnClickListener(this);
        inputData.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    inputData.setHint("最低薪资");
                    return;
                }

                if ("0".equals(s.toString())) {
                    inputData.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (maxLength > 0) {
                    if (inputData.getText().toString().length() > maxLength) {
                        inputData.setText(inputData.getText().toString().substring(0, maxLength));
                    }
                }
            }
        });

        mInputDataTwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    mInputDataTwo.setHint("最高薪资");
                    return;
                }

                if ("0".equals(s.toString())) {
                    mInputDataTwo.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mMaxLengthTwo > 0) {
                    if (mInputDataTwo.getText().toString().length() > mMaxLengthTwo) {
                        mInputDataTwo.setText(mInputDataTwo.getText().toString().substring(0, mMaxLengthTwo));
                    }
                }
            }
        });

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.time_month:
                    unitTxt = timeMonth.getText().toString();
                    unitNo = "3";
                    break;
                case R.id.time_day:
                    unitTxt = timeDay.getText().toString();
                    unitNo = "2";
                    break;
                case R.id.time_h:
                    unitTxt = timeH.getText().toString();
                    unitNo = "1";
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ok_view:
                if (!TextUtils.isEmpty(inputData.getText().toString()) && !TextUtils.isEmpty(mInputDataTwo.getText().toString())) {
//                    if (Integer.parseInt(inputData.getText().toString()) > Integer.parseInt(mInputDataTwo.getText().toString())) {
//                        MToast.showToast(mContext, "最低薪资不能大于最高薪资");
//                        return;
//                    }
//
//                    if (Integer.parseInt(inputData.getText().toString()) == Integer.parseInt(mInputDataTwo.getText().toString())) {
//                        MToast.showToast(mContext, "最低薪资不能等于最高薪资");
//                        return;
//                    }

                    if (Integer.parseInt(inputData.getText().toString()) >= Integer.parseInt(mInputDataTwo.getText().toString())) {
                        MToast.showToast(mContext, "最低薪资不能大于等于最高薪资");
                        return;
                    }

                    if (onResultClick != null) {
                        onResultClick.result(inputData.getText().toString() + "-" + mInputDataTwo.getText().toString(), unitTxt, unitNo);
                        dismiss();
                    }
                } else if (!TextUtils.isEmpty(inputData.getText().toString()) || !TextUtils.isEmpty(mInputDataTwo.getText().toString())){
                    if (onResultClick != null) {
                        onResultClick.result(inputData.getText().toString() + "-" + mInputDataTwo.getText().toString(), unitTxt, unitNo);
                        dismiss();
                    }
                }else {
                    MToast.showToast(mContext, "请输入薪资标准");
                }


                break;
            case R.id.reset_view:
                cancel();
                dismiss();
                break;
            default:
                break;
        }
    }

    private void cancel() {
        inputData.setText("");
        timeH.setChecked(true);
        unitTxt = "小时";
        unitNo = "1";
        onResultClick.result(inputData.getText().toString(), "", "0");
    }

    public void setPopData(String dataTxt, String dataTxtTwo, String unitTxt) {
        this.unitTxt = unitTxt;
        if (unitTxt == null) {
            this.unitTxt = "小时";
        }

        if (this.unitTxt.equals(month)) {
            unitNo = "1";
            timeH.setChecked(true);
        } else if (this.unitTxt.equals(quarter)) {
            unitNo = "2";
            timeDay.setChecked(true);
        } else if (this.unitTxt.equals(year)) {
            unitNo = "3";
            timeMonth.setChecked(true);
        }

        inputData.setText(dataTxt);
        mInputDataTwo.setText(dataTxtTwo);
    }

    /**
     * 设置数据
     *
     * @param unit 后面是一个数组，最大是三个，就算写了三个以上也没有用，因为根本就只会取到三个，分别对应后面的三个提示，小时，天，月，三个选择框的文字提示，这里这样设计是为了应对不同需求
     */
    public void setPopDataChange(String... unit) {
        if (unit.length > 0) {
            if (!TextUtils.isEmpty(unit[0])) {
                month = unit[0];
            } else {
                month = "小时";
            }
            timeH.setText(month);
        }

        if (unit.length > 1) {
            if (!TextUtils.isEmpty(unit[1])) {
                quarter = unit[1];
            } else {
                quarter = "天";
            }
            timeDay.setText(quarter);
        }

        if (unit.length > 2) {
            if (!TextUtils.isEmpty(unit[2])) {
                year = unit[2];
            } else {
                year = "月";
            }
            timeMonth.setText(year);
        }

        unitTxt = unit[0];
    }


    IDisMiss iDisMiss;

    public interface IDisMiss {
        void OnClose();
    }

    public void OnDisMiss(IDisMiss iDisMiss) {
        this.iDisMiss = iDisMiss;
    }


    public static class Builder {

        SalaryRegionPopWindow salaryRegionPopWindow;

        public Builder(Context context) {
            salaryRegionPopWindow = new SalaryRegionPopWindow(context);
        }

        public Builder(Context context, int maxLengthOne, int maxLengthTwo) {
            salaryRegionPopWindow = new SalaryRegionPopWindow(context, maxLengthOne, maxLengthTwo);
        }

        public SalaryRegionPopWindow.Builder setCheckText(String... unit) {
            salaryRegionPopWindow.setPopDataChange(unit);
            return this;
        }

        public SalaryRegionPopWindow.Builder setDefaultData(String dataTxt, String dataTxtTwo, String unitTxt) {
            salaryRegionPopWindow.setPopData(dataTxt, dataTxtTwo, unitTxt);
            return this;
        }

        public SalaryRegionPopWindow.Builder setBgView(View view) {
            salaryRegionPopWindow.setBgView(view);
            return this;
        }

        public SalaryRegionPopWindow.Builder setSoftInputMode(int mode) {
            salaryRegionPopWindow.setSoftInputMode(mode);
            return this;
        }

        public SalaryRegionPopWindow build() {
            return salaryRegionPopWindow;
        }
    }
}
